package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.entity.basedata.BaseProject;
import org.jeecg.entity.basedata.BaseProjectInfo;
import org.jeecg.modules.basedata.mapper.BaseProjectInfoMapper;
import org.jeecg.modules.basedata.mapper.BaseProjectMapper;
import org.jeecg.modules.basedata.service.IBaseProjectService;
import org.jeecg.modules.system.model.TreeSelectModel;
import org.jeecg.vo.basedata.BaseProjectPage;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.model.SysDepartTreeModel;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 项目
 * @Author: jeecg-boot
 * @Date:   2019-06-18
 * @Version: V1.0
 */
@Service
public class BaseProjectServiceImpl extends ServiceImpl<BaseProjectMapper, BaseProject> implements IBaseProjectService {

	@Autowired
	private BaseProjectMapper baseProjectMapper;
	@Autowired
	private BaseProjectInfoMapper baseProjectInfoMapper;


	@Override
	public List<SysDepartTreeModel> queryProjectTreeList() {
		LoginUser sysUser = SsoUtil.getLoginUser();
		LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
		query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		query.eq(SysDepart::getDepartProperty, CommonConstant.DEPART_PROPERTY_COMPANY);
		query.orderByAsc(SysDepart::getDepartOrder);
		List<SysDepart> list= baseProjectMapper.queryProjectTreeList(sysUser.getId());
		// 调用wrapTreeDataToTreeList方法生成树状数据
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list);
		return listResult;
	}



	@Override
	public List<SysDepartTreeModel> queryProjectTreeLists() {
		LoginUser sysUser = SsoUtil.getLoginUser();
		List<SysDepart> list= baseProjectMapper.queryProjectTreeLists(sysUser.getId());
		// 调用wrapTreeDataToTreeList方法生成树状数据
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list);
		return listResult;
	}

	@Override
	public List<SysDepartTreeModel> queryProjectTreeListOne() {
		LoginUser sysUser = SsoUtil.getLoginUser();
		LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
		query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		query.eq(SysDepart::getDepartProperty, CommonConstant.DEPART_PROPERTY_COMPANY);
		query.orderByAsc(SysDepart::getDepartOrder);
		List<SysDepart> list= baseProjectMapper.queryProjectTreeListOne(sysUser.getId());

		// 调用wrapTreeDataToTreeList方法生成树状数据
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list);
		return listResult;
	}

	@Override
	public BaseProjectPage selectByMainId(String mainId) {
		return baseProjectMapper.selectByMainId(mainId);
	}
	
	@Override
	@Transactional
	public void saveMain(BaseProject baseProject, BaseProjectInfo baseProjectInfo) {
		baseProjectMapper.insert(baseProject);
		baseProjectInfo.setId(baseProject.getId());
		baseProjectInfoMapper.insert(baseProjectInfo);
	}

	@Override
	@Transactional
	public void updateMain(BaseProject baseProject,BaseProjectInfo baseProjectInfo) {
		baseProjectMapper.updateById(baseProject);
		
		//1.先删除子表数据
		//baseProjectInfoMapper.deleteByMainId(baseProject.getId());

		//2.子表数据重新插入
		baseProjectInfo.setId(baseProject.getId());
		baseProjectInfoMapper.updateById(baseProjectInfo);
	}

	@Override
	@Transactional
	public void delMain(String id) {
		//baseProjectInfoMapper.deleteByMainId(id);
		baseProjectMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			//baseProjectInfoMapper.deleteByMainId(id.toString());
			baseProjectMapper.deleteById(id);
		}
	}

	@Override
	public List<BaseProject> findProjectList(String userId) {
		return baseProjectMapper.findProjectList(userId);
	}

	@Override
	public BaseProject getProjectById(String projectId) {
		return baseProjectMapper.getProjectById(projectId);
	}

	@Override
	public BaseProject getProjectByName(String projectName) {
		return baseProjectMapper.getProjectByName(projectName);
	}

	@Override
	public Page<BaseProject> listByParams(Page<BaseProject> page,BaseProject baseProject) {
		return page.setRecords(baseProjectMapper.listByParams(page,baseProject));
	}

	@Override
	public List<BaseProject> findProjectAll() {
		return baseProjectMapper.findProjectAll();
	}

	@Override
	public Integer getFloorCount(String id) {
		return baseProjectMapper.getFloorCount(id);
	}

	@Override
	public Integer getFloorCounts(String[] ids) {
		return baseProjectMapper.getFloorCounts(ids);
	}

	@Override
	public List<SysDepartTreeModel> queryProjectTreeByKeywords(String keyWord,List<String> projectIds) {
		if(projectIds == null || projectIds.size() == 0 ){
			return new ArrayList<>();
		}
		return baseProjectMapper.queryProjectTreeByKeywords(keyWord,projectIds);
	}

	@Override
	public List<SysDepartTreeModel> queryOfficeTreeByKeywords(String keyWord, List<String> officeIds) {
		if(officeIds == null || officeIds.size() == 0 ){
			return new ArrayList<>();
		}
		return baseProjectMapper.queryOfficeTreeByKeywords(keyWord,officeIds);
	}
	@Override
	public List<SysDepartTreeModel> getByProjectIds(BaseProject baseProject){
		List<SysDepartTreeModel> list=	baseProjectMapper.getByProjectIds(baseProject);
		if(list.size() > 0) {
			for (SysDepartTreeModel children : list) {
				children.setIsLeaf(false);
				children.setTitle(children.getDepartName());
				children.setKey(children.getId());
				children.setValue(children.getId());
				List<SysDepartTreeModel> listAll = baseProjectMapper.queryUnitTreeList(children.getId());
				for (SysDepartTreeModel mode : listAll) {
					mode.setKey(mode.getId());
					mode.setValue(mode.getId());
					mode.setIsLeaf(true);
					mode.setTitle(mode.getDepartName());
				}
				children.setChildren(listAll);
			}
		}
		return list;
	}

	@Override
	public List<TreeSelectModel> getByFloorTreeList(String projectId) {
		return baseProjectMapper.getByFloorTreeList(projectId);
	}

//	@Override
//	public String queryProjectName(String projectId) {
//		return baseProjectMapper.queryProjectName(projectId);
//	}

	@Override
	public List<SysDepartTreeModel> queryTenantList(String floorId){
		return baseProjectMapper.queryTenantList(floorId);
	}

	@Override
	public Integer queryTenantListCount(String floorId){
		return baseProjectMapper.queryTenantListCount(floorId);
	}

	@Override
	public Integer queryCustomerListCount(String floorId){
		return baseProjectMapper.queryCustomerListCount(floorId);
	}

	@Override
	public List<SysDepartTreeModel> queryCustomerList(String floorId){
		return baseProjectMapper.queryCustomerList(floorId);
	}

	@Override
	public List<SysDepartTreeModel> queryOutCustomer(String floorId){
		return baseProjectMapper.queryOutCustomer(floorId);
	}


}
